﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AUV5.Web.Service
{
    public partial class UserDetail : PageBaseService
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string userid = Request["userid"]??"";
            string username = Request["username"]??"";
            string email = Request["email"]??"";
            string secretkey = Request["secretkey"] ?? "";

            if (!IsValidKey(secretkey, userid, username, email))
            {
                Response.Write("112");//密钥不正确
                return;
            }

            Model.UserInfo user = null;
            if (Request["userid"] != null)
            {
                user= BLL.UserInfoBLL.Instance.GetDetail(userid);
            }
            else if(Request["username"]!=null)
            {
                user = BLL.UserInfoBLL.Instance.GetDetailByName(username);
            }
            else if (Request["email"]!=null)
            {
                user = BLL.UserInfoBLL.Instance.GetDetailByEmail(email);
            }
            else
            {
                Response.Write("111");//传入参数不正确 
                return;
            }

            if (user != null)
            {
                GetFullUser(ref user);
                Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(user));
            }
            else
            {
                Response.Write("301");//无效的用户
            }

        }

        private bool IsValidKey(string secretkey, string userid, string username, string email)
        {
            if (secretkey_enable)
            {
                if (string.IsNullOrEmpty(secretkey))
                {
                    return false;
                }
                else
                {
                    string finalkey =base.EncryptMD5(public_key + current_day + "id" + userid + "name" + username + "email" + email).Substring(6,22);
                    return secretkey == finalkey;
                }
            }
            else
            {
                return true;
            }
        }





        //获取完整的用户信息
        private void GetFullUser(ref Model.UserInfo user)
        {
            user.UserProfile = BLL.UserFieldBLL.Instance.GetDetailByUserID(user.UserID);



            #region//将信息的ID替换为真实数据
            //省
            Model.GBAD province = BLL.GBADBLL.GetInstance().GetDetail(user.Province);
            user.Province = province != null ? province.ADName : "未知";
            //市
            Model.GBAD city = BLL.GBADBLL.GetInstance().GetDetail(user.City);
            user.City = city != null ? city.ADName : "未知";
            //爱好
            string[] idlist = user.UserProfile.Hobby.Split(',');
            string hobbylist = "";
            for (int i = 0; i < idlist.Length; i++)
            {
                if (idlist[i] != "")
                {
                    Model.BaseData hobby = BLL.BaseDataBLL.Instance.GetDetail(idlist[i]);
                    if (i == 0)
                    {
                        hobbylist = hobby != null ? hobby.DataValue : "未知";    
                    }
                    else
                    {
                        hobbylist +="," + (hobby != null ? hobby.DataValue : "未知");
                    }
                }
            }
            user.UserProfile.Hobby = hobbylist;
            //收入
            Model.BaseData income = BLL.BaseDataBLL.Instance.GetDetail(user.UserProfile.Income);
            user.UserProfile.Income = income != null ? income.DataValue : "未知";
            //行业
            Model.BaseData industry = BLL.BaseDataBLL.Instance.GetDetail(user.UserProfile.Industry);
            user.UserProfile.Industry = industry != null ? industry.DataValue : "未知";
            //婚姻
            Model.BaseData marrige = BLL.BaseDataBLL.Instance.GetDetail(user.UserProfile.Marrige);
            user.UserProfile.Marrige = marrige != null ? marrige.DataValue : "未知";
            #endregion
        }
    }
}